home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch8 / PlayFast.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-05-28  |  8.3 KB  |  284 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmPlayFast 
  4.    Caption         =   "PlayFast"
  5.    ClientHeight    =   3825
  6.    ClientLeft      =   1680
  7.    ClientTop       =   975
  8.    ClientWidth     =   5850
  9.    LinkTopic       =   "Form1"
  10.    PaletteMode     =   1  'UseZOrder
  11.    ScaleHeight     =   255
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   390
  14.    Begin VB.TextBox txtNumFrames 
  15.       Height          =   285
  16.       Left            =   1560
  17.       TabIndex        =   8
  18.       Text            =   "100"
  19.       Top             =   120
  20.       Width           =   375
  21.    End
  22.    Begin VB.OptionButton optRunType 
  23.       Caption         =   "Looping"
  24.       Height          =   255
  25.       Index           =   2
  26.       Left            =   360
  27.       TabIndex        =   6
  28.       Top             =   1560
  29.       Width           =   1095
  30.    End
  31.    Begin VB.OptionButton optRunType 
  32.       Caption         =   "Reversing"
  33.       Height          =   255
  34.       Index           =   1
  35.       Left            =   360
  36.       TabIndex        =   5
  37.       Top             =   1200
  38.       Width           =   1095
  39.    End
  40.    Begin VB.OptionButton optRunType 
  41.       Caption         =   "One time"
  42.       Height          =   255
  43.       Index           =   0
  44.       Left            =   360
  45.       TabIndex        =   4
  46.       Top             =   840
  47.       Value           =   -1  'True
  48.       Width           =   1095
  49.    End
  50.    Begin VB.PictureBox picFrame 
  51.       AutoRedraw      =   -1  'True
  52.       AutoSize        =   -1  'True
  53.       Height          =   375
  54.       Index           =   0
  55.       Left            =   1560
  56.       ScaleHeight     =   21
  57.       ScaleMode       =   3  'Pixel
  58.       ScaleWidth      =   21
  59.       TabIndex        =   2
  60.       Top             =   1560
  61.       Visible         =   0   'False
  62.       Width           =   375
  63.    End
  64.    Begin VB.CommandButton cmdStart 
  65.       Caption         =   "Start"
  66.       Default         =   -1  'True
  67.       Enabled         =   0   'False
  68.       Height          =   375
  69.       Left            =   600
  70.       TabIndex        =   1
  71.       Top             =   2040
  72.       Width           =   855
  73.    End
  74.    Begin VB.PictureBox picCanvas 
  75.       Height          =   3810
  76.       Left            =   2040
  77.       ScaleHeight     =   250
  78.       ScaleMode       =   3  'Pixel
  79.       ScaleWidth      =   250
  80.       TabIndex        =   0
  81.       Top             =   0
  82.       Width           =   3810
  83.    End
  84.    Begin MSComDlg.CommonDialog dlgOpenFile 
  85.       Left            =   1560
  86.       Top             =   960
  87.       _ExtentX        =   847
  88.       _ExtentY        =   847
  89.       _Version        =   393216
  90.       CancelError     =   -1  'True
  91.    End
  92.    Begin VB.Label Label2 
  93.       Caption         =   "Frames to load:"
  94.       Height          =   255
  95.       Left            =   120
  96.       TabIndex        =   7
  97.       Top             =   120
  98.       Width           =   1455
  99.    End
  100.    Begin VB.Label lblResults 
  101.       Height          =   615
  102.       Left            =   120
  103.       TabIndex        =   3
  104.       Top             =   2640
  105.       Width           =   1815
  106.    End
  107.    Begin VB.Menu mnuFile 
  108.       Caption         =   "&File"
  109.       Begin VB.Menu mnuFileOpen 
  110.          Caption         =   "&Open..."
  111.          Shortcut        =   ^O
  112.       End
  113.    End
  114. Attribute VB_Name = "frmPlayFast"
  115. Attribute VB_GlobalNameSpace = False
  116. Attribute VB_Creatable = False
  117. Attribute VB_PredeclaredId = True
  118. Attribute VB_Exposed = False
  119. Option Explicit
  120. Private NumImages As Integer
  121. Private MaxImage As Integer
  122. Private Playing As Boolean
  123. Private NumPlayed As Long
  124. ' Run the animation forward and backward until
  125. ' Playing is False.
  126. Private Sub PlayImagesBackAndForth()
  127.     ' Start the animation.
  128.     Do While Playing
  129.         PlayImagesOnce
  130.         If Not Playing Then Exit Do
  131.         PlayImagesReversed
  132.     Loop
  133. End Sub
  134. ' Run the animation until Playing is false.
  135. Private Sub PlayImagesLooping()
  136.     ' Start the animation.
  137.     Do While Playing
  138.         PlayImagesOnce
  139.     Loop
  140. End Sub
  141. ' Run the animation once or until Playing is False.
  142. Private Sub PlayImagesOnce()
  143. Dim i As Integer
  144.     ' Start the animation.
  145.     For i = 0 To NumImages - 1
  146.         ' Display the next frame.
  147.         picCanvas.Picture = picFrame(i).Picture
  148.         DoEvents
  149.         NumPlayed = NumPlayed + 1
  150.         If Not Playing Then Exit For
  151.     Next i
  152. End Sub
  153. ' Run the animation reversed once or until Playing
  154. ' is False.
  155. Private Sub PlayImagesReversed()
  156. Dim i As Integer
  157.     ' Start the animation.
  158.     For i = NumImages - 1 To 0 Step -1
  159.         ' Display the next frame.
  160.         picCanvas.Picture = picFrame(i).Picture
  161.         DoEvents
  162.         NumPlayed = NumPlayed + 1
  163.         If Not Playing Then Exit For
  164.     Next i
  165. End Sub
  166. ' Load the images.
  167. Private Sub LoadImages(file_name As String)
  168. Dim base As String
  169. Dim i As Integer
  170.     ' Get the base file name.
  171.     base = Left$(file_name, Len(file_name) - 5)
  172.     ' See how many frames the user wants to load.
  173.     If Not IsNumeric(txtNumFrames.Text) Then _
  174.         txtNumFrames.Text = Format$(10)
  175.     NumImages = CInt(txtNumFrames.Text)
  176.     ' Create any needed picture boxes.
  177.     For i = MaxImage + 1 To NumImages - 1
  178.         Load picFrame(i)
  179.     Next i
  180.     ' Get rid of any that are no longer needed.
  181.     For i = NumImages To MaxImage
  182.         Unload picFrame(i)
  183.     Next i
  184.     MaxImage = NumImages - 1
  185.     ' Load the images.
  186.     On Error GoTo LoadPictureError
  187.     i = 0
  188.     Do While i < NumImages
  189.         lblResults.Caption = Format$(i + 1)
  190.         lblResults.Refresh
  191.         picFrame(i).Picture = LoadPicture(base & Format$(i) & ".bmp")
  192.         i = i + 1
  193.     Loop
  194.     picCanvas.AutoSize = True
  195.     picCanvas.Picture = picFrame(0).Image
  196.     picCanvas.AutoSize = False
  197.     lblResults.Caption = ""
  198.     txtNumFrames.Text = Format$(NumImages)
  199.     Exit Sub
  200. LoadPictureError:
  201.     ' We ran out of images early.
  202.     NumImages = i
  203.     txtNumFrames.Text = Format$(NumImages)
  204.     Resume Next
  205. End Sub
  206. ' Run the animation until Playing is false.
  207. Private Sub PlayImages()
  208. Dim start_time As Long
  209. Dim stop_time As Long
  210.     ' Start the appropriate animation.
  211.     NumPlayed = 0
  212.     start_time = GetTickCount
  213.     If optRunType(0).Value Then
  214.         PlayImagesOnce
  215.     ElseIf optRunType(1).Value Then
  216.         PlayImagesBackAndForth
  217.     Else
  218.         PlayImagesLooping
  219.     End If
  220.     ' Display results.
  221.     stop_time = GetTickCount
  222.     lblResults.Caption = _
  223.         Format$(NumPlayed) & " frames/" & _
  224.         Format$((stop_time - start_time) / 1000#, "0.00") & _
  225.         " sec" & vbCrLf & vbCrLf & _
  226.         Format$(CSng(NumPlayed) / ((stop_time - start_time) / 1000#), "0.00") & _
  227.         " frames/sec"
  228. End Sub
  229. ' Start or stop playing.
  230. Private Sub cmdStart_Click()
  231.     If Playing Then
  232.         Playing = False
  233.         cmdStart.Caption = "Stopped"
  234.         cmdStart.Enabled = False
  235.     Else
  236.         cmdStart.Caption = "Stop"
  237.         lblResults.Caption = ""
  238.         DoEvents
  239.         Playing = True
  240.         PlayImages
  241.         Playing = False
  242.         cmdStart.Caption = "Start"
  243.         cmdStart.Enabled = True
  244.     End If
  245. End Sub
  246. Private Sub Form_Load()
  247.     dlgOpenFile.InitDir = App.Path
  248. End Sub
  249. ' Load new image files.
  250. Private Sub mnuFileOpen_Click()
  251. Dim file_name As String
  252.     ' Let the user select a file.
  253.     On Error Resume Next
  254.     dlgOpenFile.FileName = "*_0.BMP"
  255.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  256.     dlgOpenFile.ShowOpen
  257.     If Err.Number = cdlCancel Then
  258.         Exit Sub
  259.     ElseIf Err.Number <> 0 Then
  260.         Beep
  261.         MsgBox "Error selecting file.", , vbExclamation
  262.         Exit Sub
  263.     End If
  264.     On Error GoTo 0
  265.     Screen.MousePointer = vbHourglass
  266.     DoEvents
  267.     file_name = Trim$(dlgOpenFile.FileName)
  268.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  269.         - Len(dlgOpenFile.FileTitle) - 1)
  270.     Caption = "PlayFast [" & dlgOpenFile.FileTitle & "]"
  271.     ' Load the pictures.
  272.     On Error GoTo LoadError
  273.     LoadImages file_name
  274.     On Error GoTo 0
  275.     cmdStart.Enabled = True
  276.     Screen.MousePointer = vbDefault
  277.     Exit Sub
  278. LoadError:
  279.     Screen.MousePointer = vbDefault
  280.     MsgBox "Error " & Format$(Err.Number) & _
  281.         " opening file '" & file_name & "'" & vbCrLf & _
  282.         Err.Description
  283. End Sub
  284.